import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const rootDir = path.join(__dirname, '..');
const distDir = path.join(rootDir, 'dist');

// 创建dist目录
if (!fs.existsSync(distDir)) {
  fs.mkdirSync(distDir);
}

// 复制必要文件到dist目录
const filesToCopy = [
  'package.json',
  '.env'
];

filesToCopy.forEach(file => {
  const src = path.join(rootDir, file);
  const dest = path.join(distDir, file);
  
  if (fs.existsSync(src)) {
    fs.copyFileSync(src, dest);
    console.log(`已复制文件: ${file}`);
  }
});

// 复制src目录
const srcDir = path.join(rootDir, 'src');
const destSrcDir = path.join(distDir, 'src');

if (!fs.existsSync(destSrcDir)) {
  fs.mkdirSync(destSrcDir, { recursive: true });
}

function copyDirectory(source, destination) {
  const files = fs.readdirSync(source, { withFileTypes: true });
  
  for (const file of files) {
    const srcPath = path.join(source, file.name);
    const destPath = path.join(destination, file.name);
    
    if (file.isDirectory()) {
      if (!fs.existsSync(destPath)) {
        fs.mkdirSync(destPath, { recursive: true });
      }
      copyDirectory(srcPath, destPath);
    } else {
      fs.copyFileSync(srcPath, destPath);
    }
  }
}

copyDirectory(srcDir, destSrcDir);
console.log('已复制 src 目录');

// 创建 uploads 目录
const uploadsDir = path.join(distDir, 'uploads');
if (!fs.existsSync(uploadsDir)) {
  fs.mkdirSync(uploadsDir);
  console.log('已创建 uploads 目录');
}

console.log('打包完成！文件已准备好用于生产环境部署。'); 